home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / database / postgres / appgen-0.2-a / appgen-0 / AppGEN / src / tools / defgen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-11  |  14.6 KB  |  460 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_FIELDS 64
  5.  
  6. char *freadln(FILE*);
  7. char *caps(char[]);
  8. char *getval(char[], FILE*, int, FILE*);
  9. int countcomma(char[]);
  10.  
  11. struct tables {
  12.     char name[17];
  13.     int  no_fields;
  14.     char title[256];
  15.     char summary[128];
  16.     struct field {
  17.         char name[32];
  18.         char type[10];    
  19.         int length;
  20.         int primary_key;
  21.         char foreign[128]; } field[MAX_FIELDS];
  22.         }; 
  23.  
  24. void sqlout(struct tables,FILE*);
  25. void cout(struct tables,FILE*);
  26.  
  27. char pghost[128];
  28. char pgdatabase[32];
  29. char pgport[10];
  30. char pgoptions[10];
  31. char pgtty[10];
  32. char cgi_url[128];
  33. char doc_url[128];
  34. char pgdir[256];
  35. char httpdhome[256];
  36. char rootname[256];
  37.  
  38. main (int argc, char *argv[])
  39. {
  40.     FILE *input,*app,*sql;
  41.     char line[256],cgi[256],appname[256],sqlname[256];
  42.     int a,done,b,c,tables=1;
  43.     char numb[6];
  44.     struct tables table[64];
  45.     if (argc!=2) {
  46.         printf("Usage: defgen <filename.def>\n");
  47.         exit(1);
  48.         }
  49.     input=fopen(argv[1],"rt");
  50.     if (input==NULL) {
  51.         fprintf(stderr,"Cannot open %s\n",argv[1]);
  52.         exit(1);
  53.         }
  54.     for (a=0; ((a<strlen(argv[1]))&&(argv[1][a]!='.')); a++) {
  55.         rootname[a]=argv[1][a];
  56.         } 
  57.     strcpy(appname,rootname);
  58.     strcat(appname,".app");
  59.     strcpy(sqlname,rootname);
  60.     strcat(sqlname,".sql");
  61.     app=fopen(appname,"wt");
  62.     if (input==NULL) {
  63.         fprintf(stderr,"Cannot create %s\n",appname);
  64.         exit(1);
  65.         }
  66.     sql=fopen(sqlname,"wt");
  67.     if (input==NULL) {
  68.         fprintf(stderr,"Cannot create %s\n",sqlname);
  69.         exit(1);
  70.         }
  71.     
  72.  
  73.     strcpy(pgdir,getval("PGDIR",input,1,app));
  74.     strcpy(httpdhome,getval("HTTPD-DIR",input,1,app));
  75.     strcpy(pghost,getval("PGHOST",input,1,app));
  76.     strcpy(pgdatabase,getval("PGDATABASE",input,1,app));
  77.     strcpy(pgport,"NULL");
  78.     strcpy(pgoptions,"NULL");
  79.     strcpy(pgtty,"NULL");
  80.     strcpy(cgi_url,getval("CGI-URL",input,1,app));
  81.     strcpy(doc_url,getval("DOC-URL",input,1,app));
  82. /* GLOBAL   */
  83.     fprintf(app,"[GLOBAL]\n");
  84.     fprintf(app,"    SET pgdir AS \"%s\"\n", pgdir);
  85.     fprintf(app,"    SET pghost AS \"%s\"\n", pghost);
  86.     fprintf(app,"    SET pgdatabase AS \"%s\"\n", pgdatabase);
  87.     fprintf(app,"    SET httpd-dir AS \"%s\"\n", httpdhome);
  88.     fprintf(app,"    SET cgi-url AS \"%s\"\n", cgi_url);
  89.     fprintf(app,"    SET doc-url AS \"%s\"\n", doc_url);
  90.     fprintf(app,"    SET src-dir AS \".\"\n\n");
  91.  
  92.  
  93.     while(!feof(input)) {
  94.         for (a=0; a<17; a++) table[tables].name[a]=0;
  95.         strcpy(table[tables].name,getval("TABLE-NAME",input,tables,app));
  96.         if (strlen(table[tables].name)>1) {
  97.             strcpy(table[tables].title,getval("SCREEN-TITLE",input,tables,app));
  98.             strcpy(table[tables].summary,getval("SUMMARY-LIST",input,tables,app));
  99.             getval("FIELDS",input,tables,app);
  100.             a=0;
  101.             done=0;
  102.             do {
  103.                 strcpy(line,freadln(input));
  104.                 if ((line[0]=='E')&&(line[1]=='N')&&(line[2]=='D')) done=1;
  105.                 if (!done) {
  106.                     for (b=0; ((b<strlen(line))&&(line[b]<97)); b++); 
  107.                     for (c=b; ((c<strlen(line))&&(line[c]!=' ')); c++) table[tables].field[a].name[c-b]=line[c];
  108.                     for (c=c; ((c<strlen(line))&&(line[c]<'a')); c++);
  109.                     for (b=c; ((b<strlen(line))&&(line[b]!=' ')&&(line[b]!='(')); b++) table[tables].field[a].type[b-c]=line[b];
  110.                     if (line[b]=='(') {
  111.                         strcpy(numb,"       ");
  112.                         for (c=b+1; ((b<strlen(line))&&(line[c]!=')')); c++) numb[c-b-1]=line[c]; 
  113.                         table[tables].field[a].length=atoi(numb);
  114.                         }
  115.                     else table[tables].field[a].length=1;
  116.                     for (c=b; c<strlen(line); c++) if (line[c]=='*') table[tables].field[a].primary_key=1;
  117.                     for (c=0; c<128; c++) table[tables].field[a].foreign[c]=0;
  118.                     for (c=b; c<strlen(line); c++) if (line[c]=='$') {
  119.                         c+=2;
  120.                         for (b=c; b<strlen(line); b++) table[tables].field[a].foreign[b-c]=line[b];   
  121.                         }
  122.                     a++;
  123.                     }
  124.             } while (!done);
  125.         table[tables].no_fields=a;
  126.         printf("Parsing Table %d: %s\n",tables,table[tables].name);
  127.         for (a=0; a<table[tables].no_fields; a++) printf("  Attribute: %d, %s %s %d\n",a,table[tables].field[a].name,table[tables].field[a].type,table[tables].field[a].length);
  128.         sqlout(table[tables],sql);        
  129.         cout(table[tables],app);
  130.         b=0;
  131.         for (a=0; ((a<strlen(cgi_url))&&(b<3)); a++) {
  132.             if (cgi_url[a]=='/') b++;
  133.             }
  134.         for (b=a; b<strlen(cgi_url); b++) cgi[b-a]=cgi_url[b];
  135.         tables++;
  136.         }
  137.     }    
  138.     fclose(sql);
  139.     fclose(input);
  140.     fclose(app);
  141.     
  142. }
  143.  
  144. char *freadln(FILE *input)
  145. {
  146.     int a,c;
  147.     char output[256];
  148.     for (a=0; a<256; a++) output[a]=0;
  149.     a=0;
  150.     do {
  151.         c=fgetc(input);
  152.         output[a]=c;
  153.         a++;
  154.         } while((a<256) && (c!='\n') && (!feof(input))); 
  155.     a--;
  156.     output[a]=0;
  157.     return(output);
  158. }
  159.  
  160. char *getval(char* var1, FILE *input, int from, FILE *out)
  161. {
  162.     int a,b,done;
  163.     char line[256],var[256];
  164.     char output[256];
  165.     strcpy(var,var1);
  166.     fseek(input, 0, SEEK_SET);
  167.     for (b=0; b<from; b++) {
  168.         do  {
  169.             strcpy(line,freadln(input));
  170.             done=1;    
  171.             for (a=0; a<strlen(var); a++) if (line[a]!=var[a]) done=0;
  172.             } while ((!feof(input)) && (!done));
  173.         }
  174.  
  175.     a++;
  176.     if (done) {
  177.         for (b=a; b<strlen(line); b++) output[b-a]=line[b];
  178.         output[b-a]=0;  
  179.         return(output);
  180.         }
  181.     else {
  182.         printf("Can't find %s, exiting...\n",var);
  183.         fprintf(out,"\n[]");
  184.         exit(0);
  185.         }
  186. }
  187.  
  188. int countcomma(char* var1)
  189. {
  190.     int a,b;
  191.     char var[256];
  192.     strcpy(var,var1);
  193.     b=0;
  194.     for (a=0; a<strlen(var); a++) if (var[a]==',') b++;
  195.     return(b);
  196. }
  197.  
  198. char *caps(char* var1)
  199. {
  200.     int a;
  201.     char var[256];
  202.     strcpy(var, var1);
  203.     for (a=0; a<strlen(var); a++) if (var[a]>=97) var[a]-=97-65;
  204.     return(var);
  205. }
  206.         
  207. void sqlout(struct tables table, FILE *sql)
  208. {
  209.     int a;
  210.     fprintf(sql,"CREATE TABLE %s (\n",table.name);
  211.     fprintf(sql,"    recnum int,\n");
  212.     for (a=0; a<table.no_fields; a++) {
  213.         fprintf(sql,"    %s %s",table.field[a].name,table.field[a].type);
  214.         if (table.field[a].length>1) fprintf(sql,"(%d)",table.field[a].length);
  215.         if (a<table.no_fields-1) fprintf(sql,",\n");
  216.         else fprintf(sql,"); \n\n");
  217.         }
  218. }
  219.  
  220. void cout(struct tables table, FILE *cout) 
  221. {
  222.     char filename[32],foreign_field[32],foreign_table[32];
  223.     char spare[256];
  224.     int a,and,b,c,clear_result;
  225.     fprintf(cout,"[MODULE %s]\n",table.name);
  226.     fprintf(cout,"    SET default_process AS \"clear\"\n\n");
  227.  
  228. /* FORM */
  229.     fprintf(cout,"[FORM %s]\n",table.name);
  230.     fprintf(cout,"    SET title AS \"%s\"\n", table.title);
  231.     fprintf(cout,"    DEFINE FORM {\n");
  232.     fprintf(cout,"        recnum : 10 : \"\" : HIDDEN\n");
  233.     for (a=0; a<table.no_fields; a++) {
  234.         if (strcmp("int",table.field[a].type)==0) table.field[a].length=10;
  235.         if (strcmp("date",table.field[a].type)==0) table.field[a].length=12;
  236.         if (strcmp("float",table.field[a].type)==0) table.field[a].length=12;
  237.         fprintf(cout,"        %s : %d : \"%s: \" : ",table.field[a].name,table.field[a].length,table.field[a].name);
  238.         if (table.field[a].foreign[0]!=0) fprintf(cout,"PICK PROCESS \"pick_%s\"\n",table.field[a].name);
  239.         else fprintf(cout,"TEXT\n");
  240.         }
  241.     fprintf(cout,"        } IN COLUMN\n");
  242.     fprintf(cout,"    DEFINE TOOLBAR {\n");
  243.     fprintf(cout,"        \"ADD\" : ALL : PROCESS \"add\"\n");
  244.     fprintf(cout,"        \"FIND\" : ALL : PROCESS \"find\"\n");
  245.     fprintf(cout,"        \"UPDATE\" : DATA : PROCESS \"update\"\n");
  246.     fprintf(cout,"        \"DELETE\" : DATA : PROCESS \"delete\"\n");
  247.     fprintf(cout,"        \"CLEAR\" : ALL : PROCESS \"clear\"\n");
  248.     fprintf(cout,"        \"EXIT\" : ALL : MENU \"%s\"\n",rootname);
  249.     fprintf(cout,"        }\n\n");
  250.  
  251. /* SURE */
  252.     fprintf(cout,"[FORM sure]\n");
  253.     fprintf(cout,"    SET title AS \"Are you sure you want to delete ?\"\n");
  254.     fprintf(cout,"    DEFINE FORM {\n");
  255.     fprintf(cout,"        recnum : 10 : \"\" : HIDDEN\n");        
  256.     for (a=0; a<table.no_fields; a++) if (table.field[a].primary_key==1) {
  257.         if (strcmp("int",table.field[a].type)==0) table.field[a].length=10;
  258.         if (strcmp("date",table.field[a].type)==0) table.field[a].length=12;
  259.         fprintf(cout,"        %s : %d : \"%s: \" : TEXT\n",table.field[a].name,table.field[a].length,table.field[a].name);
  260.         }
  261.     fprintf(cout,"        }\n    DEFINE TOOLBAR {\n");
  262.     fprintf(cout,"        \"YES\" : ALL : PROCESS \"yes\"\n");
  263.     fprintf(cout,"        \"NO\" : ALL : PROCESS \"clear\"\n");    
  264.     fprintf(cout,"        }\n\n");
  265.  
  266. /* ADD */
  267.     fprintf(cout,"[PROCESS add]\n");
  268.     fprintf(cout,"    DATA results\n");
  269.     fprintf(cout,"    INT no_rows\n");
  270.     fprintf(cout,"    INT new_recnum\n");
  271.     fprintf(cout,"    SQL \"SELECT * FROM %s WHERE ",table.name);
  272.     and=0;    
  273.     for (a=0; a<table.no_fields; a++) if (table.field[a].primary_key==1) {
  274.         if (and==1) fprintf(cout," AND"); 
  275.         fprintf(cout," %s=$input.%s",table.field[a].name,table.field[a].name);
  276.         and=1;
  277.         }
  278.     fprintf(cout,";\" TO results\n");
  279.     fprintf(cout,"    ROWS results TO no_rows\n");
  280.     fprintf(cout,"    IF no_rows > 0 THEN BEGIN\n");
  281.     for (a=0; ((a<table.no_fields) && (table.field[a].primary_key!=1)); a++); 
  282.     fprintf(cout,"        DISPLAY %s WITH input ERROR \"Duplicate Record\" ON %d\n",table.name,a+1);
  283.     fprintf(cout,"        END\n");
  284.     for (a=0; a<table.no_fields; a++) if (table.field[a].foreign[0]!=0) {
  285.         fprintf(cout,"    SQL \"SELECT %s WHERE ",table.field[a].foreign);
  286.         for (b=0; ((b<strlen(table.field[a].foreign))&&(table.field[a].foreign[b]!=' ')); b++) foreign_field[b]=table.field[a].foreign[b];
  287.         foreign_field[b]=0;
  288.         c=b;
  289.         for (b=c; ((b<strlen(table.field[a].foreign))&&(table.field[a].foreign[b]!=' ')); b++) foreign_table[b-c]=table.field[a].foreign[b];
  290.         c=b;
  291.         for (b=c; b<strlen(table.field[a].foreign); b++) foreign_table[b-c]=table.field[a].foreign[b];
  292.         foreign_table[b+1]=0;
  293.         fprintf(cout,"%s=",foreign_field);
  294.         fprintf(cout,"$input.%s ;\" TO results\n",table.field[a].name);
  295.         fprintf(cout,"    ROWS results TO no_rows\n");
  296.         fprintf(cout,"    IF no_rows = 0 THEN BEGIN\n");
  297.         fprintf(cout,"        DISPLAY %s WITH input ERROR \"Foreign Key Not Found\" ON %d\n",table.name,a+1);
  298.         fprintf(cout,"        END\n");
  299.         }
  300.     fprintf(cout,"    SQL \"SELECT max(recnum) FROM %s;\" TO results\n",table.name);
  301.     fprintf(cout,"    CALC ( results.0.0 + 1 ) TO new_recnum\n");
  302.     
  303.  
  304.     fprintf(cout,"    SQL \"INSERT INTO %s (recnum",table.name);
  305.     and=1;
  306.     for (a=0; a<table.no_fields; a++) {
  307.         if (and==1) fprintf(cout,",");
  308.         fprintf(cout,"%s",table.field[a].name);
  309.         and=1;
  310.         }
  311.     fprintf(cout,") VALUES ($new_recnum ");
  312.     and=1;
  313.     for (a=0; a<table.no_fields; a++) {
  314.         if (and==1) fprintf(cout,",");
  315.         fprintf(cout," ");
  316.         if (!((table.field[a].type[0]=='i') || (table.field[a].type[0]=='f'))) fprintf(cout,"\'");
  317.         fprintf(cout,"$input.%s",table.field[a].name);
  318.         if (!((table.field[a].type[0]=='i') || (table.field[a].type[0]=='f'))) fprintf(cout,"\'");
  319.         and=1;
  320.         }
  321.     fprintf(cout,");\"\n");
  322.     fprintf(cout,"    DISPLAY %s\n\n",table.name);
  323.  
  324.  
  325. /* UPDATE */
  326.     fprintf(cout,"[PROCESS update]\n");
  327.     fprintf(cout,"    DATA results\n");
  328.     fprintf(cout,"    INT no_rows\n");
  329.     
  330.  
  331.     for (a=0; a<table.no_fields; a++) if (table.field[a].foreign[0]!=0) {
  332.         fprintf(cout,"    SQL \"SELECT %s WHERE ",table.field[a].foreign);
  333.         for (b=0; ((b<strlen(table.field[a].foreign))&&(table.field[a].foreign[b]!=' ')); b++) foreign_field[b]=table.field[a].foreign[b];
  334.         foreign_field[b]=0;
  335.         c=b;
  336.         for (b=c; ((b<strlen(table.field[a].foreign))&&(table.field[a].foreign[b]!=' ')); b++) foreign_table[b-c]=table.field[a].foreign[b];
  337.         c=b;
  338.         for (b=c; b<strlen(table.field[a].foreign); b++) foreign_table[b-c]=table.field[a].foreign[b];
  339.         foreign_table[b+1]=0;
  340.         fprintf(cout,"%s=",foreign_field);
  341.         fprintf(cout,"$input.%s ;\" TO results\n",table.field[a].name);
  342.         fprintf(cout,"    ROWS results TO no_rows\n");
  343.         fprintf(cout,"    IF no_rows = 0 THEN BEGIN\n");
  344.         fprintf(cout,"        DISPLAY %s WITH input ERROR \"Foreign Key Not Found\" ON %d\n",table.name,a+1);
  345.         fprintf(cout,"        END\n");
  346.         }
  347.     fprintf(cout,"    SQL \"UPDATE %s SET ",table.name);
  348.     and=0;
  349.     for (a=0; a<table.no_fields; a++) {
  350.         if (and==1) fprintf(cout,",");
  351.         fprintf(cout,"%s=",table.field[a].name);
  352.         if (!((table.field[a].type[0]=='i') || (table.field[a].type[0]=='f'))) fprintf(cout,"\'");
  353.         fprintf(cout,"$input.%s",table.field[a].name);
  354.         if (!((table.field[a].type[0]=='i') || (table.field[a].type[0]=='f'))) fprintf(cout,"\'");
  355.         and=1;
  356.         }
  357.     fprintf(cout," WHERE recnum = $input.recnum ;\"\n");
  358.     fprintf(cout,"    DISPLAY %s\n\n",table.name);
  359.  
  360.  
  361. /* DELETE */
  362.     fprintf(cout,"[PROCESS delete]\n");
  363.     fprintf(cout,"    DISPLAY sure WITH input\n\n");
  364.     
  365. /* CLEAR */ 
  366.     fprintf(cout,"[PROCESS clear]\n");
  367.     fprintf(cout,"    DISPLAY %s\n\n",table.name);
  368.     
  369. /* YES */    
  370.     fprintf(cout,"[PROCESS yes]\n");
  371.     fprintf(cout,"    SQL \"DELETE FROM %s WHERE recnum = $input.recnum ",table.name);
  372.     fprintf(cout,";\"\n");
  373.     fprintf(cout,"    DISPLAY %s\n\n",table.name);
  374.  
  375. /* FIND */    
  376.     fprintf(cout,"[PROCESS find]\n");
  377.     fprintf(cout,"    DATA results\n");
  378.     fprintf(cout,"    INT and\n");
  379.     fprintf(cout,"    STRING query\n");
  380.     fprintf(cout,"    MOVE 0 TO and\n");
  381.     fprintf(cout,"    MOVE \"SELECT recnum, %s FROM %s WHERE \" TO query\n",table.summary,table.name);
  382.     for (a=0; a<table.no_fields; a++) if (table.field[a].type[0]=='c') {
  383.         fprintf(cout,"    IF NOT BLANK(input.%s) THEN BEGIN\n",table.field[a].name);
  384.         fprintf(cout,"        IF and = 1 THEN APPEND query \" AND \"\n");
  385.         if ((table.field[a].type[0]=='f')||(table.field[a].type[0]=='i')) {
  386.             fprintf(cout,"        APPEND query \"%s = \" input.%s\n",table.field[a].name,table.field[a].name);
  387.              }
  388.         else {
  389.             fprintf(cout,"        APPEND query \"%s like \'%%\" input.%s \"%%\'\"\n",table.field[a].name,table.field[a].name);
  390.          
  391.             }
  392.         fprintf(cout,"        MOVE 1 TO and\n");
  393.         fprintf(cout,"        END\n");
  394.         }
  395.     fprintf(cout,"    APPEND query \"ORDER BY %s;\"\n",table.summary);
  396.     fprintf(cout,"    SQL query TO results\n");
  397.     fprintf(cout,"    OUTPUT find_list WITH results\n\n");
  398.  
  399.  
  400. /* FIND_DISPLAY */
  401.     
  402.     fprintf(cout,"[PROCESS find_display]\n");
  403.     fprintf(cout,"    DATA results\n");
  404.     fprintf(cout,"    SQL \"SELECT recnum, ");
  405.     and=0;
  406.     for (a=0; a<table.no_fields; a++) {
  407.         if (and==1) fprintf(cout,",");
  408.         fprintf(cout,"%s",table.field[a].name);
  409.         and=1;
  410.         }
  411.     fprintf(cout," FROM %s WHERE recnum=$input.recnum ",table.name);
  412.     fprintf(cout,";\" TO results\n");
  413.     fprintf(cout,"    DISPLAY %s WITH results\n\n",table.name);
  414.  
  415.  
  416. /* PICK DISPLAY */
  417.     fprintf(cout,"[PROCESS pick_display]\n");
  418.     fprintf(cout,"    DISPLAY %s WITH input\n\n",table.name);
  419.  
  420.  
  421. /* PICKS */
  422.     for (a=0; a<table.no_fields; a++) if (table.field[a].foreign[a]!=0) {
  423.         fprintf(cout,"[PROCESS pick_%s]\n",table.field[a].name);
  424.         fprintf(cout,"    DATA results\n");
  425.         fprintf(cout,"    SQL \"SELECT %s ; \" TO results\n",table.field[a].foreign);
  426.         fprintf(cout,"    OUTPUT pick_%s WITH results\n\n",table.field[a].name);
  427.         fprintf(cout,"[OUTPUT pick_%s]\n",table.field[a].name);
  428.         fprintf(cout,"    SET title AS \"Available choices:\"\n");
  429.         fprintf(cout,"    SET type AS screen\n");
  430.         fprintf(cout,"    DEFINE  OUTPUT {\n");
  431.         fprintf(cout,"        %s : LEFT : \"%s\"\n",table.field[a].name,table.field[a].name);
  432.         fprintf(cout,"        } LINK TO PROCESS pick_display WITH VALUE %s\n\n",table.field[a].name);
  433.         }    
  434.                     
  435.  
  436. /* OUTPUT */
  437.  
  438.     fprintf(cout,"[OUTPUT find_list]\n");
  439.     fprintf(cout,"    SET title AS \"Search Results\"\n");
  440.     fprintf(cout,"    SET type AS screen\n");
  441.     fprintf(cout,"    DEFINE OUTPUT {\n");
  442.     fprintf(cout,"        recnum : RIGHT : \"Record\"\n");
  443.     b=0;
  444.     for (a=0; a<strlen(table.summary); a++) {
  445.         if ((table.summary[a]!=',') && (table.summary[a]!=' ')) {
  446.             spare[b]=table.summary[a];
  447.             b++;
  448.             }
  449.         if (table.summary[a]==',') {
  450.             spare[b]=0;
  451.             b=0;
  452.             fprintf(cout,"        %s : LEFT : \"%s\"\n",spare,spare);
  453.             }
  454.         }
  455.     spare[b]=0;
  456.     fprintf(cout,"        %s : LEFT : \"%s\"\n",spare,spare);        
  457.     fprintf(cout,"        } LINK TO PROCESS find_display WITH VALUE recnum");    
  458.     fprintf(cout,"\n\n\n");    
  459. }
  460.